package com.msmall.community.bbs.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.msmall.base.model.BaseObject;
import com.msmall.base.util.Constants;
import com.msmall.community.bbs.util.BbsUtil;


@Entity
@Table(name="bbs_thread")
public class Thread extends BaseObject implements Serializable {

	private static final long serialVersionUID = -6004052420003415088L;

	@Id
	@Column(name="thread_id")
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Long threadId;

	@Column(name="nickname")
	private String nickname;
	
	@Column(name="title")
	private String title;

	@Lob
	@Basic(fetch=FetchType.LAZY)
	@Column(name="content")
	private String content;

	@Column(name="file_path")
	private String filePath; // 文件路径

	@Column(name="file_name")
	private String fileName; //文件名称
	
	@Column(name="file_ext")
	private String fileExt; // 文件扩展名
	
	@Column(name="file_flag")
	private String fileFlag; // 文件标识，目前生产了什么文件
	
	@Column(name="enabled")
	private Short enabled;

	@Column(name="create_time")
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;

	@Column(name="signature")
	private String signature;

	/**
	 * WWW 1 , WAP 2
	 */
	@Column(name="source")
	private Short source;
	
	@Column(name="topic_id")
	private Long topicid;

	@Column(name="user_id")
	private Long userid;
	
	public Thread() {
		super();
	}

	public Long getThreadId() {
		return this.threadId;
	}

	public void setThreadId(Long threadId) {
		this.threadId = threadId;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getTitle() {
		return this.title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return this.content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Short getEnabled() {
		return this.enabled;
	}

	public void setEnabled(Short enabled) {
		this.enabled = enabled;
	}

	public Date getCreateTime() {
		return this.createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Short getSource() {
		return source;
	}

	public void setSource(Short source) {
		this.source = source;
	}

	public String getSignature() {
		return this.signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileExt() {
		return fileExt;
	}

	public void setFileExt(String fileExt) {
		this.fileExt = fileExt;
	}

	public String getFileFlag() {
		return fileFlag;
	}

	public void setFileFlag(String fileFlag) {
		this.fileFlag = fileFlag;
	}
	public Long getTopicid() {
		return topicid;
	}

	public void setTopicid(Long topicid) {
		this.topicid = topicid;
	}

	public Long getUserid() {
		return userid;
	}

	public void setUserid(Long userid) {
		this.userid = userid;
	}

	public String getWmlPath(){
		return getAbsolutePath(Constants.USER_AGENT_TYPE_WML);
	}
	
	public String getHtmlPath(){
		return getAbsolutePath(Constants.USER_AGENT_TYPE_HTML);
	}
	
	private String getAbsolutePath(String uat){
		String errorPage = "thread_notexist.jsp";
		if(this.enabled.compareTo(Short.valueOf("1")) != 0){
			return errorPage;
		}
		String path = Constants.FILE_SEP + BbsUtil.getPrefixPath(uat) + filePath + fileName + fileExt;
		if(!existFile(uat)){ // 文件不存在
		}
		return path;
	}
	private boolean existFile(String uat){
		if(fileFlag == null || "".equals(fileFlag))
			return false;
		if(uat.equals(Constants.USER_AGENT_TYPE_WML)){
			String value = fileFlag.substring(fileFlag.length() -1, fileFlag.length());
			if(value.equals("1")){
				return true;
			}
			return false;
		}
		if(uat.equals(Constants.USER_AGENT_TYPE_HTML)){
			if(fileFlag.length() < 2)
				return false;
			String value = fileFlag.substring(fileFlag.length() -2, fileFlag.length()-1);
			if(value.equals("1")){
				return true;
			}
			return false;
		}
		return false;
	}

	
	@Override
	public boolean equals(Object o) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}

}
